package org.example.pims.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.commons.constant.Constants;
import org.example.commons.entity.SysMessage;
import org.example.commons.websoket.MessageHandler;
import org.example.user.statistic.dto.SysHealthRecordDTO;
import org.example.user.statistic.service.ISysHealthRecordService;
import org.example.user.warning.service.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;

/**
 * Description：
 *
 * @author bothin
 */
@RestController
public class TestController {

    @Autowired
    private MessageHandler messageHandler;

    @Autowired
    ISysMessageService messageService;

    @Autowired
    ISysHealthRecordService healthRecordService;

    @RequestMapping("test")
    public String test() throws IOException {
        SysMessage sysMessage = messageService.queryById(1L);
        messageHandler.sendMessage(0L,new ObjectMapper().writeValueAsString(sysMessage));
        HashMap<String, Object> map = new HashMap<>();
        map.put("type","message");
        map.put("data",sysMessage);
        messageHandler.sendMessage(3L, Constants.MAPPER.writeValueAsString(map) );
        messageHandler.sendMessage(1002L,"你好");
        return "访问测试成功";
    }

    @RequestMapping("test2")
    public String test2() throws IOException {
        SysHealthRecordDTO sysHealthRecordDTO = healthRecordService.queryById(1L);
        HashMap<String, Object> map = new HashMap<>();
        map.put("type","record");
        map.put("data",sysHealthRecordDTO);
        messageHandler.sendMessage(3L, Constants.MAPPER.writeValueAsString(map) );
        return "访问测试成功";
    }
}
